package org.cgs.web.server.admin;

import java.io.IOException;
import java.sql.*;

import javax.servlet.http.*;

import org.apache.log4j.Logger;
import org.cgs.web.database.DatabaseConnector;

public final class EditInstructor extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    private static final Logger LOGGER = Logger.getLogger(EditInstructor.class);

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        // Get School info from the web page
        String courseCode = request.getParameter("courseCode") == null ? "" : request.getParameter("courseCode");
        String username = request.getParameter("username") == null ? "" : request.getParameter("username");
        String password = request.getParameter("password") == null ? "" : request.getParameter("password");
        String role = request.getParameter("role") == null ? "" : request.getParameter("role");

        // Get ready to make DB call
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rSet = null;
        try
        {
            // Get instructor account from DB matching input
            con = DatabaseConnector.getInstance().getConnection();
            pstmt = con.prepareStatement("SELECT * FROM instructorAuthorization WHERE courseCode=? AND netID = ?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
            pstmt.setString(1, courseCode);
            pstmt.setString(2, username);
            rSet = pstmt.executeQuery();

            if (rSet.next())
            {
                rSet.updateString("role", role);
                rSet.updateString("password", password);
                rSet.updateRow();
            }
        } // End try block
        catch (Exception e)
        {
            LOGGER.error("Exception thrown while atttempting to edit an instructor", e);
        }
        finally
        {
            try
            {
                // Close the ResultSet
                if (rSet != null)
                {
                    rSet.close();
                }
            }
            catch (Exception e)
            {
                LOGGER.error("Exception cleaning up result set", e);
            }

            try
            {
                // Close the PreparedStatement
                if (pstmt != null)
                {
                    pstmt.close();
                }
            }
            catch (Exception e)
            {
                LOGGER.error("Exception cleaning up prepared statement", e);
            }

            try
            {
                // Close the connection
                if (con != null)
                {
                    con.close();
                }
            }
            catch (Exception e)
            {
                LOGGER.error("Exception cleaning up connection to the database", e);
            }
        } // End finally

        response.sendRedirect("admin/instructor.jsp?netID=" + username);
    } // End method
} // End class
